// pages/handle/addInfo/addInfo.js
var app = getApp();
var FormCheckRule = {

  idCardRule1: /^[0-9]{14}([0-9]|X|x)$/,
  idCardRule2: /^[0-9]{17}([0-9]|X|x)$/,
  nameRule: /^[\u4e00-\u9fa5]{2,12}$/,
  telRule: /^[1]{1}[0-9]{10}$/,
  verifyCodeRule: /^[a-zA-Z0-9]{1,6}$/,
  sendVerifyCodeSuccess: "验证码发送成功！",
  cardNbrNullMessage: "请输入身份证号！",
  cardNbrErrorMessage: "请输入正确的身份证号！",

  cardNameNullMessage: "请输入车主姓名！",
  cardNameErrorMessage: "请输入正确的车主姓名！",

  telephoneNullMessage: "请输入手机号码！",
  telephoneErrorMessage: "请输入正确的手机号码！",

  verifyCodeNullMessage: "请输入验证码！",
  cardPictureNullMessage: "请上传手持身份证照片！",
  policyPictureNullMessage: "请上传交强险保单副本！",
  num: 0
};

Page({
  data: {
    cardName: "",
    cardNum: "",
    verifyCodeText: "获取验证码",
    cardPicture: "",
    policyPicture: "",
    telephone: "",
    verifyCode:"",
      verifyCodeClear:"0"
  },
  onLoad() {
      var page =  app.getStorageSync(app.globalData.cachePageKey);
      if(page.cardName!=''&&page.cardName!=null) {
          if (FormCheckRule.nameRule.test(page.cardName)) {
              this.setData({"cardNameIconType": "success"});
          } else if (page.cardName.length > 0) {
              this.setData({"cardNameIconType": "error"});
          } else {
              this.setData({"cardNameIconType": ""});
          }
      }

      if(page.cardNum!=''&&page.cardNum!=null){
          if( FormCheckRule.idCardRule1.test(page.cardNum)&& page.cardNum.length == 15 ) {
              this.setData({"cardNumIconType": "success"});
          } else if (FormCheckRule.idCardRule2.test(page.cardNum)&&page.cardNum.length == 18) {
              this.setData({"cardNumIconType": "success"});
          } else if(page.cardNum.length>0){
              this.setData({"cardNumIconType": "error"});
          }else{
              this.setData({"cardNumIconType": ""});
          }
      }

      if(page.telephone!=''&&page.telephone!=null) {
          if (FormCheckRule.telRule.test(page.telephone)) {
              this.setData({"telephoneIconType": "success"});
          } else if (page.telephone.length > 0) {
              this.setData({"telephoneIconType": "error"});
          } else {
              this.setData({"telephoneIconType": ""});
          }
      }
  },
  onReady() {
    // 页面渲染完成

  },
  onShow() {
      var cardPicture = app.getStorageSync(app.globalData.cardPictureKey) || "";
      var policyPicture = app.getStorageSync(app.globalData.policyPictureKey) || "";
      var page =  app.getStorageSync(app.globalData.cachePageKey);
      this.setData({
          "cardName":page.cardName,
          "cardNum":page.cardNum,
          "telephone":page.telephone,
          "cardPicture":cardPicture,
          "policyPicture":policyPicture,
          "verifyCode":page.verifyCodeClear=='1'?'':page.verifyCodeClear,
          "agree":page.agree
      });
  },
    onHide(){
        var data = app.getStorageSync(app.globalData.cachePageKey);
        data['cardName'] = this.data.cardName;
        data['cardNum'] = this.data.cardNum;
        data['cardPicture'] =  app.getStorageSync(app.globalData.cardPictureKey) || "";
        data['policyPicture'] = app.getStorageSync(app.globalData.policyPictureKey) || "";
        data['telephone'] = this.data.telephone;
        app.setStorageSync(app.globalData.cachePageKey, data);
    },
  onUnload() {
      var data = app.getStorageSync(app.globalData.cachePageKey);
      data['cardName'] = this.data.cardName;
      data['cardNum'] = this.data.cardNum;
      data['cardPicture'] =  app.getStorageSync(app.globalData.cardPictureKey) || "";
      data['policyPicture'] = app.getStorageSync(app.globalData.policyPictureKey) || "";
      data['telephone'] = this.data.telephone;
      app.setStorageSync(app.globalData.cachePageKey, data);
  },
  infoInput(e) {
    var id = e.currentTarget.id;
    var value = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
    if (id == "cardName") {
      this.setData({ "cardName": value });
      if (value == '') {
        this.setData({ "cardNameIconType": "success" });
      } else if (FormCheckRule.nameRule.test(value)) {
        this.setData({ "cardNameIconType": "success" });
      } else if (value.length > 0) {
        this.setData({ "cardNameIconType": "error" });
      } else {
        this.setData({ "cardNameIconType": "" });
      }
    } else if (id == "cardNum") {
      value = value.substr(0, 18);
      this.setData({ "cardNum": value });
      if (value == '') {
        this.setData({ "cardNumIconType": "success" });
      } else if (value.length == 15 && FormCheckRule.idCardRule1.test(value)) {
        this.setData({ "cardNumIconType": "success" });
      } else if (value.length == 18 && FormCheckRule.idCardRule2.test(value)) {
        this.setData({ "cardNumIconType": "success" });
      } else if (value.length > 0) {
        this.setData({ "cardNumIconType": "error" });
      } else {
        this.setData({ "cardNumIconType": "" });
      }

    } else if (id == "telephone") {
      value = value.substr(0, 11);
      this.setData({ "telephone": value });
      if (value == '') {
        this.setData({ "telephoneIconType": "success" });
      } else if (FormCheckRule.telRule.test(value)) {
        this.setData({ "telephoneIconType": "success" });
      } else if (value.length > 0) {
        this.setData({ "telephoneIconType": "error" });
      } else {
        this.setData({ "telephoneIconType": "" });
      }
    }
  },
  // 图片上传
  uploadTap(e) {
    var type = e.currentTarget.dataset.type;
    var value = e.currentTarget.dataset.value;
    app.navigateTo({
      url: '../upload/upload?type=' + type + '&uploadImgSrc=' + value
    });
  },
  // 获取验证码
  verifyCodeTap(e) {
    var pageObj = this;
    var telephone = pageObj.data.telephone;
    var btnText = pageObj.data.verifyCodeText;
    if (btnText != "获取验证码" && btnText != "重新发送") {
      return false;
    }
    if (telephone == '') {
      app.errorToast(FormCheckRule.telephoneNullMessage);
      return false
    } else if (!FormCheckRule.telRule.test(telephone)) {
      app.errorToast(FormCheckRule.telephoneErrorMessage);
      return false;
    }
    var opt = {   //请求广告
      url: app.globalData.host + "/wx/an/vc.ajax",
      data: { phoneNumber: telephone },
      sucFn: function (result) {
        if (result.body != null) {
          app.errorToast(FormCheckRule.sendVerifyCodeSuccess);
          if (btnText === "获取验证码" || btnText === "重新发送") {
            btnText = '59秒后获取';    //第一次59秒
            var second = 59; //用来记录秒
            var SEND = setInterval(function () {
              second--;
              btnText = second + '秒后获取'; //显示时间
              pageObj.setData({
                verifyCodeText: btnText
              });
              if (second == 0) {
                btnText = "重新发送";
                clearInterval(SEND);
                pageObj.setData({
                  verifyCodeText: btnText
                });
              } //到过60秒时停止
            }, 1000); //每1000毫秒即1秒执行一次此函数
          }
        } else {
          app.toast(result.msg)
        }
      }
    };
    app.ajax(opt);
  },
  // 表单提交
  formSubmit(e) {
    var pageObj = this;
    var formData = e.detail.value;

    var cardNum = formData.cardNum;
    var cardName = formData.cardName;
    var telephone = formData.telephone;
    var verifyCode = formData.verifyCode;
    var cardPicture = formData.cardPicture;
    var policyPicture = formData.policyPicture;
    var isYueS = true;
    var cacheObj = app.getStorageSync(app.globalData.cacheOrderResKey);
    cacheObj = Object.assign({},cacheObj,formData);
    var param = Object.assign({},cacheObj);
    var flag = app.check([
      {
          value: cardName,
          errorFocusName: "cardName",
          errorScrollIntoViewName: "carCodeView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.cardNameNullMessage);
                      return false;
                  } else if (FormCheckRule.nameRule.test(value)) {
                      return true;
                  } else {
                      app.errorToast(FormCheckRule.cardNameErrorMessage);
                      return false;
                  }
              } else {
                  return true;
              }

          }
      },
      {
          value: cardNum,
          errorFocusName: "cardNum",
          errorScrollIntoViewName: "cardNameView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.cardNbrNullMessage);
                      return false;
                  } else if (value.length == 15 && FormCheckRule.idCardRule1.test(value)) {
                      return true;
                  } else if (value.length == 18 && FormCheckRule.idCardRule2.test(value)) {
                      return true;
                  } else {
                      app.errorToast(FormCheckRule.cardNbrErrorMessage);
                      return false;
                  }
              } else {
                  return true;
              }

          }
      },
      {
          value: telephone,
          errorFocusName: "telephone",
          errorScrollIntoViewName: "cardNameView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.telephoneNullMessage);
                      return false;
                  } else if (FormCheckRule.telRule.test(value)) {
                      return true;
                  } else {
                      app.errorToast(FormCheckRule.telephoneErrorMessage);
                      return false;
                  }
              } else {
                  return true;
              }

          }
      },
      {
          value: telephone,
          errorFocusName: "telephone",
          errorScrollIntoViewName: "cardNameView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.telephoneNullMessage);
                      return false;
                  } else if (FormCheckRule.telRule.test(value)) {
                      return true;
                  } else {
                      app.errorToast(FormCheckRule.telephoneErrorMessage);
                      return false;
                  }
              } else {
                  return true;
              }
          }
      },
      {
          value: verifyCode,
          errorFocusName: "verifyCode",
          errorScrollIntoViewName: "telephoneView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.verifyCodeNullMessage);
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  return true;
              }
          }
      },
      {
          value: cardPicture,
          errorFocusName: "cardPicture",
          errorScrollIntoViewName: "verifyCodeView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.cardPictureNullMessage);
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  return true;
              }
          }
      },
      {
          value: policyPicture,
          errorFocusName: "policyPicture",
          errorScrollIntoViewName: "cardPictureView",
          fn: function (value) {
              if (isYueS) {
                  if ('' == value) {
                      app.errorToast(FormCheckRule.policyPictureNullMessage);
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  return true;
              }
          }
      },
    ]);
    if (!flag) {
      return false;
    }
    if (app.globalData.openId == '' || app.globalData.openId == null || app.globalData.openId == 'null') {
      app.toastAuthFailModal();
      return false;
    }
    var opt = {
      url: app.globalData.host + "/wx/an/handling.ajax",
      data: param,
      sucFn: function (result) {
        app.setStorageSync(app.globalData.cacheOrderResKey, cacheObj);
        if(result.code=="1000"){
            app.redirectTo({ url: '../../payment/payment' });
        }
      }
    };
    app.ajax(opt);
  },
})

